package com.gudaomai.wuye.web;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.gudaomai.wuye.common.CommonConstant;
import com.gudaomai.wuye.common.CommonException;
import com.gudaomai.wuye.common.MsgConstant;
import com.gudaomai.wuye.entity.Car;
import com.gudaomai.wuye.entity.CarInput;
import com.gudaomai.wuye.entity.House;
import com.gudaomai.wuye.entity.RetEntity;
import com.gudaomai.wuye.service.ICarServ;
import com.gudaomai.wuye.web.BaseController;

import io.swagger.annotations.ApiOperation;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zenghualu
 * @since 2017-08-18
 */
@Controller
@RequestMapping("/wuye/car")
public class CarCtrl extends BaseController {
	
	@Autowired
	ICarServ carService;
	
	
	/** 
	 * add a new Car
	 * 
	 * 1. Check if all required field are filled in.
	 * 2. Check if all the inputs are valid.
	 * 3. Check if the car with the same properties does not exist in the DB.
	 * 4. Save the inputed values.
	 * 
	 *@param  
	 *@author jess
	 *return 
	 *
	 **/
	@ApiOperation("The car settings page, click the add button, add car")
	@RequestMapping(value = "/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public RetEntity addCar(@RequestBody CarInput carInput) throws Exception {
		
		RetEntity entity = new RetEntity();

		if(carService.isExist(carInput.getPlate())){
			// The car already exists in the database
			
			throw new CommonException(CommonConstant.RESULT_CODE_CREATED, 
					getMessage(MsgConstant.RET_CAR_ADD_FAILURE));
		} 
		else{
			// Create new car entity
			Car carEntity = new Car();
			// copy all user inputs to the new car entity
			BeanUtils.copyProperties(carInput, carEntity);
			
			// Insert the car entity into the database
			carService.insert(carEntity);
			
			//Return message and HTTP result code
			entity.setRetCode(CommonConstant.RESULT_CODE_OK);
			entity.setRetMessage(getMessage(MsgConstant.RET_CAR_ADD_SUCCESS));
		}	
		
		return entity;
	}
	
	/** 
	 * query car information
	 * 1. Check if the corresponding Id exists in the DB.
	 * 2. Read and return the result set from DB .
	 * 
	 *@param  id
	 *@author jess
	 *return 
	 *
	 **/
	@ApiOperation("The car settings page, click the query button, query car info")	
	@GetMapping("/id/{id}")
	public Car getCarById(@PathVariable("id") String id) throws Exception{
				
        Car car = carService.selectById(id);
		
        car.setRetCode(CommonConstant.RESULT_CODE_OK);
        car.setRetMessage(getMessage(MsgConstant.RET_CAR_SELECT_SUCCESS));
		return car;
	}
	
	/** 
	 * update the existing car information.
	 * 
	 * 1. Check if all required field are filled in.
	 * 2. Check if all the inputs are valid.
	 * 3. update the house's information in the DB.
	 * 
	 *@param 
	 *@author jess
	 *return 
	 *
	 **/
	@ApiOperation("The car settings page, click the modify button, update car")
	@RequestMapping(value = "/", method = RequestMethod.PUT)
	public RetEntity updateCar(@RequestBody CarInput carInput) throws Exception{
				
        RetEntity entity = new RetEntity();
		
		// update the car entity in the database
		// Create new car entity
		Car carEntity = new Car();
		// copy all user inputs to the new car entity
		BeanUtils.copyProperties(carInput, carEntity);
		
		carService.updateById(carEntity);
		
		//Return message and HTTP result code
		entity.setRetCode(CommonConstant.RESULT_CODE_OK);
		entity.setRetMessage(getMessage(MsgConstant.RET_CAR_UPDATE_SUCCESS));
		
		return entity;
	}
	
	
	/** 
	 * delete the existing Car
	 * 1. Check if the corresponding Id exists in the DB.
	 * 2. Delete the house information from the DB.
	 * 
	 *@param  id
	 *@author jess
	 *return 
	 *
	 **/
	@ApiOperation("The car settings page, click the delete button, delete Car")
	@RequestMapping(value="/id/{id}",method=RequestMethod.DELETE)
	public RetEntity delCarById(@PathVariable("id") String id) throws Exception{
		
        RetEntity entity = new RetEntity();
		
		carService.deleteById(id);
		
		entity.setRetCode(CommonConstant.RESULT_CODE_OK);
		entity.setRetMessage(getMessage(MsgConstant.RET_CAR_DELETE_SUCCESS));
		return entity;
	}
}
